package com.hao.rabbitmqproduce.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
@Component
public class DLXConfig {
    public static final String EXCHANGE_NAME="exchange_dlx";
    public static final String TEST_EXCHANGE_NAME="test_exchange_dlx";
    public static final String QUEUE_NAME="queue-dlx";
    public static final String TEST_QUEUE_NAME="test-queue-dlx";

    @Bean("dlxExchange")
    public Exchange dlxExchange(){
//        return new TopicExchange(TEST_EXCHANGE_NAME);
        return ExchangeBuilder.topicExchange(TEST_EXCHANGE_NAME).build();
    }

    @Bean("dlxQueue")
    public Queue dlxQueue(){
        Map<String, Object> map = new HashMap<>();
//        map.put("x-dead-letter-exchange",EXCHANGE_NAME);
//        map.put("x-dead-letter-routing-key","dlx.hehe");
//        map.put("x-message-ttl",10000);
//        map.put("x-max-length",10);
        return QueueBuilder.durable(TEST_QUEUE_NAME)
                .deadLetterExchange(EXCHANGE_NAME)
                .deadLetterRoutingKey(QUEUE_NAME)
                .ttl(10000)
                .maxLength(10)
                .build();
    }

    @Bean
    public Binding bindingDLX(@Qualifier("dlxExchange") Exchange exchange, @Qualifier("dlxQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("test.dlx.#").noargs();
    }
}
